package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.entity.ConstantsItem;
import com.neuedu.yyzx.entity.Food;
import com.neuedu.yyzx.framework.Res;
import com.neuedu.yyzx.service.IConstantsItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author keyu
 * @since 2024-04-25
 */
@RestController
@RequestMapping("/constantsItem")
public class ConstantsItemController {
    @Autowired
    IConstantsItemService constantsItemService;

    /**
     * http://localhost:8080/constantsItem/list
     * @param constantsItem
     * @return
     */
    @RequestMapping("/list")
    public Res<List<ConstantsItem>> list(ConstantsItem constantsItem) {

        QueryWrapper<ConstantsItem> queryWrapper = new QueryWrapper<>();
        return Res.ok(constantsItemService.list(queryWrapper));
    }
    //分页查询
    @RequestMapping("/page")
    public Res<Page> page(Page<ConstantsItem> page, ConstantsItem constantsItem) {
        QueryWrapper<ConstantsItem> queryWrapper = new QueryWrapper<>();
        if(constantsItem.getCode()!= null && !"".equals(constantsItem.getCode().trim())){
            queryWrapper.like("foodname" ,constantsItem.getCode() );
        }
        return Res.ok(constantsItemService.page(page,queryWrapper));
    }

    @RequestMapping("/saveOrUpdate")
    public Res  saveOrUpdate( @RequestBody ConstantsItem constantsItem){
        return  Res.ok(constantsItemService.saveOrUpdate(constantsItem));
    }


    @RequestMapping("/removeById/{id}")
    public Res  removeById(@PathVariable int id){
        return  Res.ok(constantsItemService.removeById(id));
    }


    @RequestMapping("/getById/{id}")
    public Res<Food> getById(@PathVariable  int id){
        return  Res.ok(constantsItemService.getById(id));
    }

    @RequestMapping("/getItemsByType")
    public Res<List<ConstantsItem>> getItemsByType(ConstantsItem constantsItem) {

        QueryWrapper<ConstantsItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", constantsItem.getType());
        return Res.ok(constantsItemService.list(queryWrapper));
    }

}
